﻿//#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
#include "common.h"

struct Structure
{
	char a[100];
	int b;
	float c;
};

void selection_2()
{
	int a, b, c;
	a = 5;
	c = ++a; //c=6 a=6
	b = ++c, c++, ++a, a++;//c=8 b=7 a=8
	b += a++ + c;//a=9 b=23
	printf("a =%d b =%d c=%d\n", a, b, c);
	//9 23 8
}

int different_0x(int x, int y)
{ // 求两个整数中二进制数不同的位数
	if (x >= 0 && y >= 0)
	{
		int diff = 0;
		int temp = x ^ y;
		int i;
		for (i = 0; i < 32; ++i)
		{
			if (temp & 1)
				++diff;
			temp >>= 1;
		}
		return diff;
	}
	else if (x < 0 && y >= 0)
		return different_0x(~(x - 1), y) + 1;
	else if (x >= 0 && y < 0)
		return different_0x(x, ~(y - 1)) + 1;
	else
		return different_0x(~(x - 1), ~(y - 1));
}

int count_1(int x)
{ // 统计整数的二进制数中1的个数
	if (x < 0)
		return count_1(-x) + 1;
	else
	{
		int count = 0;
		/*while (x)
		{
			count += x & 1;
			x >>= 1;
		}*/
		while (x)
		{
			x &= (x - 1);
			++count;
		}
		return count;
	}
}

int sat1;
void sizeof_compare_test()
{ // sizeof 操作符返回的是 size_t 即无符号整型
	sat1--;  // sat1 默认初始化为 0
	if (sat1 > sizeof sat1) // sat1 会被整型提升为无符号整型
		printf(">\n");
	else
		printf("<\n");
}

void print_x(int n)
{ // 打印输出传入的 n 行的 x 图案，用 ‘*’ 打印
	int i, j;
	for (i = 0; i < n; ++i)
	{
		for (j = 0; j < n; ++j)
		{
			if (i == j || i + j == n - 1)
				printf("*");
			else
				printf(" ");
		}
		printf("\n");
	}
}

void print_data(struct Structure* ps)
{ // 打印输出结构体 Structure 中各个成员的值
	printf("a = %s\n", ps->a);
	printf("b = %d\n", ps->b);
	printf("c = %f\n", ps->c);
}

void structure_pointer_test()
{ // 结构体指针数组测试
	struct Structure s3[3] = {
		{"hello", 1, 2.3}, {"world", 2, 4.5}, {"!", 3, 6.7} };
	struct Structure* ps = s3;
	printf("s3[0].a = %s\n", ps[0].a);
	printf("s3[1].b = %d\n", ps[1].b);
	printf("s3[2].c = %f\n", ps[2].c);
	printf("---------------------------\n");
	print_data(s3 + 1); // 打印输出 s3[1] 的值
}